package com.sdry.web.controller.zc;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.model.zc.ResponseResult;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.zc.ZcLoginService;

/**
 * 
 * @ClassName:    ZcLoginController   
 * @Description:  登录 1.多仓库修改  t  2019-10-31 
 * 41-44	声明一个Logger
 * 52-57	支持多仓库：登陆：后台接收值（登陆使用对应zc/login.jsp）
 * 59-76	支持多仓库：登陆：如果登陆成功判断角色，如果登陆者不是管理员且未选择仓库，登陆失败返回提示，反之保存仓库id到session
 * @Author:       zc   
 * @CreateDate:   2019年4月17日 下午3:30:30   
 * @Version:      v1.0
 */
@ResponseBody
@RequestMapping("login")
@Controller
public class ZcLoginController {
	@Resource
	public ZcLoginService ZcLoginService;
	/* ******************************多仓库修改  t  2019-11-01 开始*************************** */
	//声明一个Logger
	private final static Logger logger = LoggerFactory.getLogger(ZcBindAndUnbindController.class);
	/* ******************************多仓库修改  t  2019-11-01 结束*************************** */
	/**
	 * pc登录
	 * @param session
	 * @param zcSysUserEntity
	 * @return
	 */
	@RequestMapping("pclogin")
	/* ******************************多仓库修改  t  2019-10-31 开始***************************  */
	//多仓库修改前：public ResponseResult<Boolean> pclogin(HttpSession session,ZcSysUserEntity zcSysUserEntity){
	public ResponseResult<Boolean> pclogin(HttpSession session,ZcSysUserEntity zcSysUserEntity,Long warehouse_id,String wareHouseName){
		//日志
		logger.info("登陆接收参数：zcSysUserEntity="+JSONArray.fromObject(zcSysUserEntity)+",warehouse_id="+warehouse_id);

	/* ******************************多仓库修改  t  2019-10-31 结束***************************  */
	    ResponseResult<Boolean> login = ZcLoginService.pclogin(session,zcSysUserEntity);
	    /* ******************************多仓库修改  t  2019-10-31 开始***************************  */
	    if(login.getData()){
	    	//获取角色
			ZcSysUserEntity su = (ZcSysUserEntity)session.getAttribute("user");
			String roleName = su.getRoleName();
			String[] values = roleName.split(",");
		    List<String> list = Arrays.asList(values);
	    	//登陆者不是管理员且未选择仓库
			if(!list.contains("超级管理员") && !list.contains("管理员") && null == warehouse_id){
	    		login.setData(false);
	    		login.setMessage("请该用户选择仓库");
	    	}else{
	    		session.setAttribute("warehouse_id",warehouse_id);
	    		session.setAttribute("wareHouseName",wareHouseName);
	    	}
	    }
	    //日志
	  	logger.info("登陆发送参数：login="+JSONArray.fromObject(login));
	    /* ******************************多仓库修改  t  2019-10-31 结束***************************  */
	    return login;
	}

	/**
	 * pc登出
	 * @param request
	 * @return
	 */
	@RequestMapping("pcloginOut")
	public ModelAndView pcloginOut(HttpServletRequest request){
		Enumeration em = request.getSession().getAttributeNames();
		  while(em.hasMoreElements()){
		   request.getSession().removeAttribute(em.nextElement().toString());
		  }
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/zc/login");
		return mav;
	}
	/**
	 * app登录
	 * @param zcSysUserEntity
	 * @return
	 */
	@RequestMapping("/appLogin")
    public Object appLogin(ZcSysUserEntity zcSysUserEntity){
        Map<String,Object> map = new HashMap<>() ;
        map = ZcLoginService.appLogin(zcSysUserEntity);
        return map;
    }

}
